//
//  LWNewestCell.m
//  LWShopItem
//
//  Created by mac on 17/3/27.
//  Copyright © 2017年 LWShop.com. All rights reserved.
//

#import "LWNewestCell.h"
#import "Masonry.h"
#import "LWGroupList.h"
#import "UIImageView+WebCache.h"

@interface LWNewestCell ()

//商品图片
@property (weak, nonatomic) UIImageView *pictureImage;

//商品名称
@property (weak, nonatomic) UILabel *nameLabel;

//商品价格
@property (weak, nonatomic) UILabel *priceLabel;
/**
 商品增加按钮
 */
@property (weak, nonatomic) UIButton *increaseBtn;

/**
 商品数量
 */
@property (weak, nonatomic) UILabel *countLabel;

/**
 商品减少按钮
 */
@property (weak, nonatomic) UIButton *reduceBtn;


@end

@implementation LWNewestCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - UI

- (void)setupUI
{
    
    //图片
    UIImageView *iconImage = [[UIImageView alloc]init];
    
    [self.contentView addSubview:iconImage];
    self.pictureImage = iconImage;
    
    [iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).offset(20);
        
        make.left.equalTo(self.contentView).offset(10);
        
        make.size.mas_equalTo(CGSizeMake(80, 80));
        
    }];
    
    //名称
    UILabel *nameLabel = [UILabel new];
    nameLabel.numberOfLines = 0;
    nameLabel.font = [UIFont systemFontOfSize:15];
    
    [self.contentView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(iconImage);
        
        make.left.equalTo(iconImage.mas_right).offset(10);
        
        make.right.equalTo(self.contentView).offset(-40);
        
    }];
    
    //价格标识符
    UILabel *markLabel = [UILabel new];
    markLabel.text = @"￥:";
    markLabel.font = [UIFont systemFontOfSize:18];
    markLabel.textColor = [UIColor redColor];
    
    [self addSubview:markLabel];
    
    [markLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(iconImage.mas_bottom);
        
        make.left.equalTo(iconImage.mas_right).offset(10);
    }];
    
    
    //价格
    UILabel *priceLabel = [UILabel new];
    priceLabel.textColor = [UIColor redColor];
    
    
    [self.contentView addSubview:priceLabel];
    self.priceLabel = priceLabel;
    
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(iconImage.mas_bottom);
        
        make.left.equalTo(markLabel.mas_right).offset(2);
    }];
    
    //增加按钮
    UIButton *increaseBtn = [[UIButton alloc] init];
    [increaseBtn setImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
    [self.contentView addSubview:increaseBtn];
    self.increaseBtn = increaseBtn;
    [increaseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(priceLabel);
        make.right.equalTo(self.contentView).offset(-5);
    }];
    [increaseBtn addTarget:self action:@selector(increase_click) forControlEvents:UIControlEventTouchUpInside];
    
    //数量显示Label
    UILabel *countLabel = [[UILabel alloc] init];
    countLabel.font = [UIFont systemFontOfSize:13];
    countLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:countLabel];
    self.countLabel = countLabel;
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(increaseBtn);
        make.right.equalTo(increaseBtn.mas_left).offset(-5);
        make.width.mas_offset(15);
    }];
    
    //减少按钮
    UIButton *reduceBtn = [[UIButton alloc] init];
    [reduceBtn setImage:[UIImage imageNamed:@"v2_reduce"] forState:UIControlStateNormal];
    [self.contentView addSubview:reduceBtn];
    self.reduceBtn = reduceBtn;
    [reduceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(countLabel);
        make.right.equalTo(countLabel.mas_left).offset(-5);
    }];
    [reduceBtn addTarget:self action:@selector(reduceClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    //值改变
    [increaseBtn addTarget:self action:@selector(orderChanged) forControlEvents:UIControlEventValueChanged];
    
    //值改变
    [reduceBtn addTarget:self action:@selector(orderChanged) forControlEvents:UIControlEventValueChanged];
    
    
    
}
#pragma MARK - 检测数量变化
- (void)orderChanged
{
    NSLog(@"%@",@(self.count));
    _model.orderCount = self.count;
    
    if ([_delegate respondsToSelector:@selector(changeBadge)]) {
        [_delegate performSelector:@selector(changeBadge)];
    }
    
    if (_isIncrease == YES) {
        NSLog(@"增加 需要动画");
        if (([_delegate respondsToSelector:@selector(newCell:didFinishIncreaseFood: andStartPoint:)])) {
            
            [_delegate newCell:self didFinishIncreaseFood:_model andStartPoint:self.starP];
        }
        
    }else
    {
        NSLog(@"减少不要动画");
        if ([_delegate respondsToSelector:@selector(newCell:didFinishDecreaseFood:)]) {
            
            [_delegate newCell:self didFinishDecreaseFood:_model];
            
        }
    }
    
}


#pragma MARK - 增加按钮
- (void)increase_click
{
    NSLog(@"点击了 XXX");
    
    self.count++;
    //增加
    _isIncrease = YES;
    
    //转换坐标
    UIWindow *keyW = [UIApplication sharedApplication].keyWindow;
    _starP = [self convertPoint:self.increaseBtn.center toView:keyW];
    
    //
    [self.increaseBtn sendActionsForControlEvents:UIControlEventValueChanged];
    if ([_delegate respondsToSelector:@selector(addInToShopCarWithModel:)]) {
        [_delegate addInToShopCarWithModel:_model];
    }
    
}

- (void)reduceClick
{
    NSLog(@"点击了减少按钮");
    
    self.count--;
    //减少
    _isIncrease = NO;
    
    //
    [self.reduceBtn sendActionsForControlEvents:UIControlEventValueChanged];
    if ([_delegate respondsToSelector:@selector(deleteFromShopCarWithModel:)]) {
        [_delegate deleteFromShopCarWithModel:_model];
    }
}

//重写set 进行赋值
- (void)setCount:(NSInteger)count
{
    
    
    _count = count;
    
    NSLog(@"数量变了");
    //隐藏减少按钮，判断数量
    if (count > 0) {
        
        _reduceBtn.hidden = NO;
        _countLabel.hidden = NO;
        
    }else
    {
        
        _reduceBtn.hidden = YES;
        _countLabel.hidden = YES;
    }
    
    //数量显示给Label
    _countLabel.text = @(count).description;
    
    
}


#pragma MARK - 赋值
- (void)setModel:(LWGroupList *)model
{
    _model = model;
    
    
    [self.pictureImage  sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://shop.sdlinwang.com/%@",model.original_img]]];
    
    self.nameLabel.text = model.goods_name;
    
    self.priceLabel.text = model.shop_price;
    
    self.count = model.orderCount;
    
}



@end
